package android.FiltrosJava;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;

public class LineDeformation extends AbstractImageBasic{
	protected int blobX = 6;
	protected int blobY = 4;
	
	public LineDeformation(int sizeBlobX, int sizeBlobY){
		setLineDeformation(sizeBlobX, sizeBlobY);
	}
	
	public void setLineDeformation(int sizeBlobX, int sizeBlobY){
		blobX = Math.abs(sizeBlobX);
		blobY = Math.abs(sizeBlobY);			
	}
	
	public BufferedImage applyFilter(BufferedImage src) {		
		Graphics2D g2d = src.createGraphics(); 
		g2d.setStroke(new BasicStroke(2.0f, BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));
		
		int x, y, rx, ry;
		int dimX = src.getWidth();
		int dimY = src.getHeight();		
		WritableRaster raster = src.getRaster();    	
    	int[] imageData = new int[raster.getNumBands()];
    	int[] cor = new int[3];
		
    	int numBlob = (int) ((dimX * dimY) / 20);    	
    	
		for (int k=0; k < numBlob; k++){
			x = (int) (Math.random() * (dimX - 1));
			y = (int) (Math.random() * (dimY - 1));
			cor[0] = 0;
			cor[1] = 0;
			cor[2] = 0;
			
			for (int z = 0; z < 10; z++){
				rx = (int) (x - blobX + (Math.random() * (2 * blobX - 1)));
				ry = (int) (y - blobY + (Math.random() * (2 * blobY - 1)));
				
				if (rx < 0)
					rx = 0;
				
				if (rx >= dimX)
					rx = dimX -1;
					
				if (ry < 0)
					ry = 0;
				
				if (ry >= dimY)
					ry = dimY -1;
				
				raster.getPixel(rx,ry,imageData);
				cor[0] += imageData[0];
				cor[1] += imageData[1];
				cor[2] += imageData[2];												
			}
			
			cor[0] = (int) (cor[0] / 10);
			cor[1] = (int) (cor[1] / 10);
			cor[2] = (int) (cor[2] / 10);
			
			g2d.setColor(new Color(cor[0], cor[1], cor[2]));
			g2d.drawLine(x - blobX, y - blobY, x + blobX, y + blobY);			
		}    	    	
		
		g2d.dispose();		
		return src;		
    }
	
	/*
	public BufferedImage applyFilter(BufferedImage dst) {
		BufferedImage src = copiaImagem(dst);
		Graphics2D g2d = dst.createGraphics(); 
		//g2d.drawImage(src,0,0,null);
		g2d.setStroke(new BasicStroke(2.0f, BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));
		
		int x, y, rx, ry;
		int dimX = src.getWidth();
		int dimY = src.getHeight();		
		WritableRaster raster = src.getRaster();    	
    	int[] imageData = new int[raster.getNumBands()];
    	int[] cor = new int[3];
		
    	int numBlob = (int) ((dimX * dimY) / 20);    	
    	
		for (int k=0; k < numBlob; k++){
			x = (int) (Math.random() * (dimX - 1));
			y = (int) (Math.random() * (dimY - 1));
			cor[0] = 0;
			cor[1] = 0;
			cor[2] = 0;
			
			for (int z = 0; z < 10; z++){
				rx = (int) (x - blobX + (Math.random() * (2 * blobX - 1)));
				ry = (int) (y - blobY + (Math.random() * (2 * blobY - 1)));
				
				if (rx < 0)
					rx = 0;
				
				if (rx >= dimX)
					rx = dimX -1;
					
				if (ry < 0)
					ry = 0;
				
				if (ry >= dimY)
					ry = dimY -1;
				
				raster.getPixel(rx,ry,imageData);
				cor[0] += imageData[0];
				cor[1] += imageData[1];
				cor[2] += imageData[2];												
			}
			
			cor[0] = (int) (cor[0] / 10);
			cor[1] = (int) (cor[1] / 10);
			cor[2] = (int) (cor[2] / 10);
			
			g2d.setColor(new Color(cor[0], cor[1], cor[2]));
			g2d.drawLine(x - blobX, y - blobY, x + blobX, y + blobY);			
		}    	    	
		
		g2d.dispose();
		src = null;
		return dst;		
    }
    */
}